package com.bdcom.autotest.telnet.invoke.impl;

import com.bdcom.autotest.config.ClientStartRun;
import com.bdcom.autotest.telnet.TelnetClientForCOM;
import com.bdcom.autotest.telnet.TelnetCommunication;
import com.bdcom.autotest.telnet.invoke.DeveiceOper;
import com.bdcom.autotest.telnet.model.Result;
import com.bdcom.autotest.telnet.model.TelnetCMDObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class DevVerInfo implements DeveiceOper {
    private static final Logger logger = LoggerFactory.getLogger(DevVerInfo.class);
    @Override
    public Result executeCMD(TelnetCMDObj obj) {
        Result result=new Result();
        TelnetClientForCOM clientForCOM=null;
        try {
            TelnetCommunication communication=new TelnetCommunication();
            clientForCOM=new TelnetClientForCOM(obj.getIp(),obj.getPort());
            boolean isMaster=communication.isMaster(clientForCOM);
            if(!isMaster){
                String error="ip["+obj.getIp()+"]port["+obj.getPort()+"]is not in master,maybe rebooting";
                logger.error(error);
                result.setStatus(false);
                result.setFailedReason(error);
                return result;
            }


            Result loginResult=communication.login(obj.getUser(),obj.getPassword(),clientForCOM,obj.isMonitor());
            if(loginResult.getStatus()){
                Result enableResult = communication.enableOper(clientForCOM, obj.getEnablePassword(),obj.isMonitor());
                if(enableResult.getStatus()){
                    //String version = clientForCOM.sendCommand(obj.getCmds().get(0), 1500L);
                    String version = communication.obtainVerInfo(clientForCOM);
                    //logger.warn("version["+version+"]");
                    result.setStatus(true);
                    result.setRetMsg(version);
                    return result;

                }else{
                    return enableResult;
                }
            }else{
                return loginResult;
            }
            

        }catch (Exception e){
            result.setFailedReason(e.toString());
            return result;
        }finally {
            if(null!=clientForCOM){
                try {

                    if(!obj.isMonitor()){
                        //非monitor模式下恢复telnet连接的COM口的初始状态
                        clientForCOM.sendCommand("exit",200L);
                        clientForCOM.sendCommand("exit",200L);
                    }

                    clientForCOM.disConnect();
                } catch (Exception e) {
                    logger.error(e.toString());
                }
            }
        }


    }
    public Result obtainVerInfo(TelnetCMDObj obj){
        String cmd="show version";
        List<String> cmds=new ArrayList<String>();
        cmds.add(cmd);
        obj.setCmds(cmds);
        Result result = executeCMD(obj);
        return result;


    }
}
